#include <stdio.h>
#include <stdlib.h>
int inc(const void*a, const void*b) {
	return *(int*) a - *(int*) b;
}
void solve(int m) {
	int tot = 1, a[4], i, max, min;
	while (m != 6174) {
		for (i = 0; i < 4; i++)
			a[i] = m % 10, m /= 10;
		qsort(a, 4, sizeof(a[0]), inc);
		max = a[0] + 10 * a[1] + 100 * a[2] + 1000 * a[3];
		min = a[0] * 1000 + a[1] * 100 + a[2] * 10 + a[3];
		m = max - min;
		++tot;
	}
	printf("%d\n", tot);
}
int main(void) {
	int n, m;
	scanf("%d", &n);
	while (n--) {
		scanf("%d", &m);
		solve(m);
	}
	return EXIT_SUCCESS;
}
